package com.cl.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cl.db.dao.AppLockDao;
import com.cl.domain.AppInfo;
import com.cl.engine.AppInfoProvider;
import com.cl.safe.R;
import com.cl.utils.DensityUtil;

public class AppManagerActivity extends Activity implements OnClickListener {
	private static final String TAG = "AppManagerActivity";
	private TextView tv_avail_rom;
	private TextView tv_avail_sd;
	private LinearLayout ll_loading;
	private ListView lv_app_manager;
	private List<AppInfo> appInfos;
	private List<AppInfo> userAppInfos;
	private List<AppInfo> systemAppInfos;
	private MyAdapter adapter;
	private TextView tv_status;
	private LinearLayout ll_start;
	private LinearLayout ll_share;
	private LinearLayout ll_uninstall;
	private PopupWindow popupWindow;
	private AppInfo appInfo;
	private AppLockDao dao;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_app_manager);
		dao = new AppLockDao(this);
		tv_avail_rom = (TextView) findViewById(R.id.tv_avail_rom);
		tv_avail_sd = (TextView) findViewById(R.id.tv_avail_sd);
		tv_status = (TextView) findViewById(R.id.tv_status);
		long romSize = getAvailSpace(Environment.getDataDirectory().getAbsolutePath());
		long sdSize = getAvailSpace(Environment.getExternalStorageDirectory().getAbsolutePath());
		tv_avail_rom.setText("内存可用："+Formatter.formatFileSize(this, romSize));
		tv_avail_sd.setText("SD卡可用:"+Formatter.formatFileSize(this, sdSize));
		
		lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);
		ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
		
		fillData();//Listview填充数据
		
		// 给listview注册一个滚动的监听器
		lv_app_manager.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				
			}
			// 滚动的时候调用的方法。
		    // firstVisibleItem 第一个可见条目在listview集合里面的位置。
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				dismissPopupWindow();
				if (userAppInfos != null && systemAppInfos != null) {
					if (firstVisibleItem > userAppInfos.size()) {
						tv_status.setText("系统程序：" + systemAppInfos.size() + "个");
					} else {
						tv_status.setText("用户程序：" + userAppInfos.size() + "个");
					}
				}
			}
		});
		
		/**
		 * 设置listview的点击事件
		 */
		lv_app_manager.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long arg3) {
				dismissPopupWindow();
				if(position == 0) {
					return;
				}else if(position <= userAppInfos.size()) {//列表展示用户软件
					int newPosition = position - 1;
					appInfo = userAppInfos.get(newPosition);
				}else if(position == (userAppInfos.size() + 1)) {
					return;
				}else {//列表展示系统软件
					int newPosition = position - 2 - userAppInfos.size();
					appInfo = systemAppInfos.get(newPosition);
				}
				View contentView = View.inflate(getApplicationContext(),R.layout.popup_app_item, null);
				ll_start = (LinearLayout) contentView.findViewById(R.id.ll_start);
				ll_share = (LinearLayout) contentView.findViewById(R.id.ll_share);
				ll_uninstall = (LinearLayout) contentView.findViewById(R.id.ll_uninstall);

				ll_start.setOnClickListener(AppManagerActivity.this);
				ll_share.setOnClickListener(AppManagerActivity.this);
				ll_uninstall.setOnClickListener(AppManagerActivity.this);
				
				popupWindow = new PopupWindow(contentView, -2, -2);//-2表示包裹內容
				// 动画效果的播放必须要求窗体有背景颜色。
				// 透明颜色也是颜色
				popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
				int[] location = new int[2];
				view.getLocationInWindow(location);
				// 在代码里面设置的宽高值 都是像素。---》dip
				int dip = 60;
				int px = DensityUtil.dip2px(getApplicationContext(), dip);
				System.out.println("px=" + px);
				popupWindow.showAtLocation(parent, Gravity.LEFT | Gravity.TOP,px, location[1]);
				ScaleAnimation sa = new ScaleAnimation(0.3f, 1.0f, 0.3f, 1.0f,
						Animation.RELATIVE_TO_SELF, 0,
						Animation.RELATIVE_TO_SELF, 0.5f);
				sa.setDuration(300);
				AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
				aa.setDuration(300);
				AnimationSet set = new AnimationSet(false);
				set.addAnimation(aa);
				set.addAnimation(sa);
				contentView.startAnimation(set);
			}
		});
		//程序锁 设置条目长点击的事件监听器
		lv_app_manager.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position == 0) {
					return true;
				}else if(position <= userAppInfos.size()) {//列表展示用户软件
					int newPosition = position - 1;
					appInfo = userAppInfos.get(newPosition);
				}else if(position == (userAppInfos.size() + 1)) {
					return true;
				}else {//列表展示系统软件
					int newPosition = position - 2 - userAppInfos.size();
					appInfo = systemAppInfos.get(newPosition);
				}
				ViewHolder holder = (ViewHolder) view.getTag();
				if(dao.find(appInfo.getPackname())) {
					//开锁
					dao.delete(appInfo.getPackname());
					holder.iv_status.setImageResource(R.drawable.unlock);
				}else {
					//加锁
					dao.add(appInfo.getPackname());
					holder.iv_status.setImageResource(R.drawable.lock);
				}
				return true;
			}
		});
	}
	private void fillData() {
		//加载数据，进度条可见
		ll_loading.setVisibility(View.VISIBLE);
		new Thread(){
			public void run() {
				appInfos = AppInfoProvider.getAppInfos(getApplicationContext());
				userAppInfos = new ArrayList<AppInfo>();
				systemAppInfos = new ArrayList<AppInfo>();
				for(AppInfo appInfo :	appInfos) {
					if(appInfo.isUserApp()) {
						userAppInfos.add(appInfo);
					}else {
						systemAppInfos.add(appInfo);
					}
				}
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						//数据加载完毕，进度条不可见
						ll_loading.setVisibility(View.INVISIBLE);
						if(adapter == null) {
							adapter = new MyAdapter();
							lv_app_manager.setAdapter(adapter);
						}else {
							adapter.notifyDataSetChanged();
						}
					}
				});
			};
		}.start();
	}
	private void dismissPopupWindow() {
		// 把旧的弹出窗体关闭掉。
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
			popupWindow = null;
		}
	}

	private long getAvailSpace(String path) {
		StatFs statfs = new StatFs(path);
		long avail = statfs.getAvailableBlocks();
		long size = statfs.getBlockSize();
		long availSize = avail * size;
		return availSize;
	}
	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return userAppInfos.size()+1+systemAppInfos.size()+1;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AppInfo appInfo;
			if(position == 0) {
				TextView tv = new TextView(getApplicationContext());
				tv.setTextColor(Color.WHITE);
				tv.setBackgroundColor(Color.GRAY);
				tv.setText("用户程序：" + userAppInfos.size() + "个");
				return tv;
			}else if(position <= userAppInfos.size()) {//列表展示用户软件
				int newPosition = position - 1;
				appInfo = userAppInfos.get(newPosition);
			}else if(position == (userAppInfos.size() + 1)) {
				TextView tv = new TextView(getApplicationContext());
				tv.setTextColor(Color.WHITE);
				tv.setBackgroundColor(Color.GRAY);
				tv.setText("系统程序：" + systemAppInfos.size() + "个");
				return tv;
			}else {//列表展示系统软件
				int newPosition = position - 2 - userAppInfos.size();
				appInfo = systemAppInfos.get(newPosition);
			}
			
			View view = View.inflate(AppManagerActivity.this, R.layout.list_item_appinfo, null);
			ViewHolder holder = new ViewHolder();
			//复用contentview对象，减少id的查找次数
			if (convertView != null && convertView instanceof RelativeLayout) {
				// 不仅需要检查是否为空，还要判断是否是合适的类型去复用
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(getApplicationContext(),
						R.layout.list_item_appinfo, null);
				holder = new ViewHolder();
				holder.iv = (ImageView) view.findViewById(R.id.iv_app_icon);
				holder.iv_status = (ImageView) view.findViewById(R.id.iv_status);
				holder.tv_app_location = (TextView) view.findViewById(R.id.tv_app_location);
				holder.tv_app_name = (TextView) view.findViewById(R.id.tv_app_name);
				view.setTag(holder);
			}
			holder.tv_app_name.setText(appInfo.getName());
			if(appInfo.isInRom()) {
				holder.tv_app_location.setText("手机内存");
			}else {
				holder.tv_app_location.setText("外部存储");
			}
			holder.iv.setImageDrawable(appInfo.getIcon());
			if(dao.find(appInfo.getPackname())) {
				holder.iv_status.setImageResource(R.drawable.lock);
			}else {
				holder.iv_status.setImageResource(R.drawable.unlock);
			}
			return view;
		}
		
	}
	class ViewHolder {
		ImageView iv;
		TextView tv_app_name;
		TextView tv_app_location;
		ImageView iv_status;
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dismissPopupWindow();
	}
	/**
	 * 布局对应的点击事件
	 */
	@Override
	public void onClick(View v) {
		dismissPopupWindow();
		switch (v.getId()) {
		case R.id.ll_share:
			Log.i(TAG, "分享：" + appInfo.getName());
			shareApplication();
			break;
		case R.id.ll_start:
			Log.i(TAG, "启动：" + appInfo.getName());
			startApplication();
			break;
		case R.id.ll_uninstall:
			if (appInfo.isUserApp()) {
				Log.i(TAG, "卸载：" + appInfo.getName());
				uninstallAppliation();
			}else{
				Toast.makeText(this, "系统应用只有获取root权限才可以卸载", 0).show();
				//Runtime.getRuntime().exec("");
			}
			break;
		}
	}
	/**
	 * 分享一个应用程序
	 */
	private void shareApplication() {
		// Intent { act=android.intent.action.SEND typ=text/plain flg=0x3000000 cmp=com.android.mms/.ui.ComposeMessageActivity (has extras) } from pid 256
		Intent intent = new Intent();
		intent.setAction("android.intent.action.SEND");
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, "推荐您使用一款软件,名称叫："+appInfo.getName());
		startActivity(intent);
	}

	/**
	 * 卸载应用
	 */
	private void uninstallAppliation() {
		// <action android:name="android.intent.action.VIEW" />
		// <action android:name="android.intent.action.DELETE" />
		// <category android:name="android.intent.category.DEFAULT" />
		// <data android:scheme="package" />
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		intent.setAction("android.intent.action.DELETE");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.setData(Uri.parse("package:" + appInfo.getPackname()));
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// 刷新界面。
		fillData();
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 开启一个应用程序
	 */
	private void startApplication() {
		// 查询这个应用程序的入口activity。 把他开启起来。
		PackageManager pm = getPackageManager();
		// Intent intent = new Intent();
		// intent.setAction("android.intent.action.MAIN");
		// intent.addCategory("android.intent.category.LAUNCHER");
		// //查询出来了所有的手机上具有启动能力的activity。
		// List<ResolveInfo> infos = pm.queryIntentActivities(intent,
		// PackageManager.GET_INTENT_FILTERS);
		Intent intent = pm.getLaunchIntentForPackage(appInfo.getPackname());
		if (intent != null) {
			startActivity(intent);
		} else {
			Toast.makeText(this, "不能启动当前应用", 0).show();
		}
	}
}
